var replaceString = new Array();
$(document).ready(function() {
    String.prototype.replaceAll = function(AFindText,ARepText){  
      return this.replace(new RegExp(AFindText,"gm"),ARepText)  
   }  
    $('#replaceAdd').click(function() {
        var name1 = $('#name1').val();
        var name2 = $('#name2').val();
        if(name1 && name1.length > 0) {
            for(var i=0; i < replaceString.length; i++) {
                var t = replaceString[i];
                if(t && t['name1'] == name1) {
                    alert(name1 + ' Existed');
                    return;
                }
            }
            var theNameStr = {'name1':name1, 'name2':name2};
            replaceString.push(theNameStr);
            $('#name1').val('');
            $('#name2').val('');
            $('.filterPanel').html('<span><a class="close" title="'+ name1 +'"></a>' + name1 + ' => ' + name2 + '</span>' + $('.filterPanel').html());
            
    
    $('.close').click(function() {
        if(this.title == '') {
            replaceString = new Array();
            $('.filterPanel').html('<span><a class="close" title=""></a>Clear All</span><div class="clear"></div>');
        }
        else {
            $(this.parentNode).remove();
            removeItem(this.title);
        }
    });
        }
    });
    
    $('.close').click(function() {
        if(this.title == '') {
            replaceString = new Array();
            $('.filterPanel').html('<span><a class="close" title=""></a>Clear All</span><div class="clear"></div>');
        }
        else {
            $(this.parentNode).remove();
            removeItem(this.title);
        }
    });
    
    $('#pseudo').click(function(){
        var s1 = $('#s1').val();
        $('#s2').html('');
        for(var i=0;i<replaceString.length;i++) {
            s1 = s1.replaceAll(replaceString[i]['name1'], replaceString[i]['name2']);
        }
        var sa = s1.split('\n');
        for(var i=0;i<sa.length;i++) {
            if(sa[i] && /[a-zA-Z0-9]/.test(sa[i])) {
                pseudoOne(sa[i]);
            }
        }
    });
});

function pseudoOne(it) {
    google.language.translate(it, "en", "de", function(result) {
        if (!result.error){
            google.language.translate(result.translation, "de", "en", function(result1) {
                if(!result1.error) {
                    var s2 = $('#s2').html();
                    if(s2 == "") {
                        $('#s2').html(result1.translation);
                    }
                    else {
                       $('#s2').html(s2 + '<br/><br/>' + result1.translation);
                    }
                }
            });
        }
    });
}

function removeItem(it){
    var an = new Array();
    for(var i=0;i<replaceString.length;i++) {
        if(replaceString[i]['name1'] != it) {
            an.push(replaceString[i]);
        }
    }
    replaceString = an;
}